//
//  AdvancedBeautyViewController.swift
//  月升
//
//  Created by 拉利法 on 2024/7/10.
//  美妆进阶选项

import UIKit

class AdvancedBeautyViewController: QIBaseViewController {
    
    /// 预览图片
    var previewImg = UIImage()
    /// 原图在本地服务器地址
    var originalImgUrl = ""
    /// 原图地址
    var raw_file_path = ""
    /// 时间名称
    var event_name = ""
    /// 五官检测结果
    var wuGuanModel = QIFiveSensesResultModel()
    private var imgPreviewView = UIImageView()
    private var dataArr = [QIFacialFeaturesModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "美妆推荐"
        dataArr = [
            QIFacialFeaturesModel(title: wuGuanModel.facial_data_chinese_dict.left_eye_name, value: "左眼"),
            QIFacialFeaturesModel(title: wuGuanModel.facial_data_chinese_dict.right_eye_name, value: "右眼"),
            QIFacialFeaturesModel(title: wuGuanModel.facial_data_chinese_dict.face_name, value: "脸型"),
            QIFacialFeaturesModel(title: wuGuanModel.facial_data_chinese_dict.mouth_name, value: "嘴"),
            QIFacialFeaturesModel(title: wuGuanModel.facial_data_chinese_dict.nose_name, value: "鼻"),
            QIFacialFeaturesModel(title: wuGuanModel.facial_data_chinese_dict.left_eyebrow_name, value: "左眉"),
            QIFacialFeaturesModel(title: wuGuanModel.facial_data_chinese_dict.right_eyebrow_name, value: "右眉"),
        ]
        
        addUI()
//        getData()
    }
    

   
    private func addUI() {
        showGradientColor()
        
        imgPreviewView = UIImageView().then {
            $0.frame = CGRect(x: 15, y: NAV_HEIGHT, width: SCREEN_WIDTH - 30, height: 410)
            $0.image = previewImg
            $0.tag = 11
            $0.contentMode = .scaleAspectFill
            $0.clipsToBounds = true
            $0.addRoundedCorners(withRadius: 10)
        }
        view.addSubview(imgPreviewView)
        
        let titleL = UILabel().then({
            $0.frame = CGRect(x: 15, y: imgPreviewView.bottom + 10, width: 100, height: 22)
            $0.text = "五官识别"
            $0.textColor = Color333333
            $0.font = FONTBOLD(ofSize: 16)
        })
        view.addSubview(titleL)
        
        collectionView.frame = CGRect(x: 15, y: titleL.bottom + 10, width: SCREEN_WIDTH - 30, height: 150)
        view.addSubview(collectionView)
        
        let nextBtn = UIButton().then {
            $0.frame = CGRect(x: SCREEN_WIDTH / 2 - 55, y: collectionView.bottom + 20, width: 110, height: 36)
            $0.backgroundColor = MainColor
            $0.titleLabel?.font = FONT(ofSize: 16)
            $0.setTitle("下一步", for: .normal)
            $0.setTitleColor(.white, for: .normal)
            $0.addRoundedCorners(withRadius: 18)
            $0.addTarget(self, action: #selector(nextBtnClick), for: .touchUpInside)
        }
        view.addSubview(nextBtn)
        
    }
    
    private func getData() {
        let param = ["raw_file_path": raw_file_path]
        QINetworkManager.requestJson(urlStr: "/Skin/face", parameters: param) { json in
            print(json)
        }
    }
    
    @objc private func nextBtnClick() {
        let ctl = FilterCriteriaViewController()
        ctl.previewImg = previewImg
        ctl.wuGuanModel = wuGuanModel
        ctl.event_name = event_name
        ctl.raw_file_path = raw_file_path
        ctl.originalImgUrl = originalImgUrl
        navigationController?.pushViewController(ctl, animated: true)
    }
    

    lazy var collectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = 1
        flowLayout.minimumInteritemSpacing = 1
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.addRoundedCorners(withRadius: 10)
        collectionView.register(AdvancedBeautyViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        
        return collectionView
    }()
    
}


extension AdvancedBeautyViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cellW = (SCREEN_WIDTH - 35) / 4
        return CGSize(width: cellW, height: 74)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AdvancedBeautyViewCell
        cell.backgroundColor = UIColor.white
        if dataArr.count > indexPath.row {
            cell.model = dataArr[indexPath.row]
        } else {
            cell.model = QIFacialFeaturesModel()
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vv = AdvancedBeautySelecView(frame: CGRect(x: 65, y: NAV_HEIGHT + 150, width: SCREEN_WIDTH - 130, height: 180))
        vv.backgroundColor = UIColor(white: 0, alpha: 0.3)
        vv.typeStr = dataArr[indexPath.row].value
        vv.selectStrBlock = { str1, str2 in
            var arr = self.wuGuanModel.facial_data_code_string.components(separatedBy: ",")
            arr[indexPath.row] = str1
            self.wuGuanModel.facial_data_code_string = arr.joined(separator: ",")
            self.dataArr[indexPath.row].title = str2
            collectionView.reloadData()
        }
        QINPopUpTool.sharedInstance().popUp(with: vv, withAnimation: .none, withCanTapBgCancel: true, closeBefore: .close, bgColor: .clear)
    }
    
}


class AdvancedBeautyViewCell: UICollectionViewCell {
    
    private var topLabel = UILabel()
    private var bottomLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        addUI()
    }
    
    private func addUI() {
        topLabel = UILabel().then({
            $0.frame = CGRect(x: 0, y: 14, width: self.width, height: 25)
            $0.text = "上斜眉"
            $0.textColor = Color333333
            $0.textAlignment = .center
            $0.font = FONTBOLD(ofSize: 18)
        })
        self.addSubview(topLabel)
        
        bottomLabel = UILabel().then({
            $0.frame = CGRect(x: 0, y: topLabel.bottom + 5, width: self.width, height: 17)
            $0.text = "左眉"
            $0.textColor = Color333333
            $0.textAlignment = .center
            $0.font = FONT(ofSize: 12)
        })
        self.addSubview(bottomLabel)
    }
    
    var model = QIFacialFeaturesModel() {
        didSet {
            topLabel.text = model.title
            bottomLabel.text = model.value
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
